import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_eve_plugin/components/health/health_advice.dart';
import 'package:flutter_eve_plugin/components/health/progress_chart.dart';
import 'package:flutter_eve_plugin/components/health/recommend_drug.dart';
import 'package:flutter_eve_plugin/components/health/sys_child_header.dart';
import 'package:flutter_eve_plugin/components/health/weight_analysis.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/model/health/health_log.dart';
import 'package:flutter_eve_plugin/model/health/sys_health_property.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

class SysChildPageWidget extends StatefulWidget {
  final int type;
  final PropertyHealthDataInfo info;

  SysChildPageWidget({Key key, this.type, this.info}) : super(key: key);

  @override
  _SysChildPageWidgetState createState() => _SysChildPageWidgetState();
}

class _SysChildPageWidgetState extends State<SysChildPageWidget>
    with AutomaticKeepAliveClientMixin {
  String logType;
  HealthLogModel logDataModel;

  @override
  void initState() {
    super.initState();
    loadLogData(0);
  }

  void loadLogData(int lastItemTime, {Function() callback}) {
    ///体重：weight；神经：nervous；消耗：digestive；呼吸：respiratory；运动：motor；内分泌：endocrine,
    switch (widget.type) {
      case 0:
        logType = 'respiratory';
        break;
      case 1:
        logType = 'digestive';
        break;
      case 2:
        logType = 'motor';
        break;
      case 3:
        logType = 'nervous';
        break;
      case 4:
        logType = 'endocrine';
        break;
    }
    var req = HealthLogReq(lastItemTime, logType, 20);
    PluginEve.sendMessage(PluginConstant.httpHealthLog,
            param: req, httpType: HttpType.POST)
        .then((data) {
      if (callback != null) {
        callback();
      }
      setState(() {
        if (lastItemTime <= 0) {
          logDataModel = data;
        } else {
          HealthLogModel tmp = data;
          logDataModel?.list?.addAll(tmp?.list);
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return StoreConnector<AppState, AppStateViewModel>(converter: (store) {
      return AppStateViewModel(state: store.state);
    }, builder: (context, vm) {
      return CustomScrollView(
          physics: BouncingScrollPhysics(),
          slivers: <Widget>[
            SliverToBoxAdapter(
              child: SysChildHeaderWidget(
                type: widget.type,
                info: widget.info,
              ),
            ),
            SliverToBoxAdapter(
                child: ProgressChartWidget(
              info: widget.info,
            )),
            SliverToBoxAdapter(
                child: RecommendDrugWidget(info: widget.info,)),
            SliverToBoxAdapter(
                child: Container(
                    margin: EdgeInsets.only(
                        left: setRatio(15),
                        top: setRatio(20),
                        right: setRatio(15),
                        bottom: setRatio(30)),
                    child: HealthAdviceWidget(
                      info: widget.info,
                    ))),
            SliverToBoxAdapter(
                child: WeightAnalysisWidget(
              title: '健康分析',
              logModel: logDataModel,
              type: logType,
              logRefresh: (String type, int lastTime,
                  GlobalKey<RefreshHeaderState> headerKey) {
                loadLogData(lastTime, callback: () {
                  headerKey.currentState.onRefreshEnd();
                });
              },
              logLoadMore: (String type, int lastTime,
                  GlobalKey<RefreshFooterState> footerKey) {
                loadLogData(lastTime, callback: () {
                  footerKey.currentState.onLoadEnd();
                });
              },
            ))
          ]);
    });
  }

  @override
  bool get wantKeepAlive => true;
}
